# pylint:disable=missing-docstring,redefined-builtin
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
# This file does only contain a selection of the most common options. For a
# full list see the documentation:
# http://www.sphinx-doc.org/en/master/config

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
import inspect
import os
import sys
import unittest.mock

sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))

# Mock modules not needed for docs
sys.modules.update([('tflite_runtime', unittest.mock.MagicMock())])
sys.modules.update([('tflite_runtime.interpreter', unittest.mock.MagicMock())])


# -- Project information -----------------------------------------------------

project = 'PyCoral API'
copyright = '2020, Google LLC'
author = 'Google LLC'

# The short X.Y version
version = '2.0'
# The full version, including alpha/beta/rc tags
release = ''

# -- General configuration ---------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.intersphinx',  # Enables linking to other libs like Pillow
    'sphinx.ext.coverage',
    'sphinx.ext.napoleon',  # Converts Google-style code comments to RST
    'sphinx.ext.linkcode',  # Creates links to each API source code
]

# Autodoc configurations
autoclass_content = 'both'

# Intersfphinx config; Controls external linking to other python libraries
intersphinx_mapping = {
    'python': ('https://docs.python.org/', None),
    'PIL': ('https://pillow.readthedocs.io/en/stable/', None),
    'numpy': ('https://docs.scipy.org/doc/numpy/', None)
}

# Disable rtype return values; output return type inline with description
napoleon_use_rtype = False

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

source_parsers = {
    '.md': 'recommonmark.parser.CommonMarkParser',
}

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = ['.rst', '.md']

# The master toctree document.
master_doc = 'index'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store', 'README*']

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = None

# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = 'coral_theme'
html_theme_path = ['.']
html_file_suffix = '.md'
html_link_suffix = '/'


# Callback from sphinx.ext.linkcode to resolve links to source code
def linkcode_resolve(domain, info):
  if domain != 'py':
    return None
  if not info['module']:
    return None

  filename = info['module'].replace('.', '/')
  linespec = ''

  try:
    obj = sys.modules[info['module']]
    for part in info['fullname'].split('.'):
      obj = getattr(obj, part)
    source, lineno = inspect.getsourcelines(obj)
    linespec = '#L%d-L%d' % (lineno, lineno + len(source) - 1)
  except TypeError:
    # inspect.getsourcelines() fails this way for properties
    # But let's not link them anyway because all the links look sloppy
    return None
  except OSError:
    # inspect.getsourcelines() fails this way for namedtuples
    # So let's keep the link but they won't get line numbers
    pass

  return 'https://github.com/google-coral/pycoral/blob/master/%s.py%s' % (
      filename, linespec)
